
//DOM 元素
const postDataBtn = document.getElementById('postDataBtn');
const responseBox = document.getElementById('responseBox');

// 初始化
document.addEventListener('DOMContentLoaded', () => {
    currentPath = '';
    // loadFileList();
    setupEventListeners();
});

function setupEventListeners() {
    postDataBtn.addEventListener('click', showPostModal);
}

function showPostModal() {
    modalBody.innerHTML = `
        <h2>上传题目图片</h2>
        <div class="form-group">
            <input type="file" id="questionImage" accept="image/*"><br><br>
            <input type="text" id="questionText" placeholder="输入你的问题"><br><br>
            <label for="questionType">题目类型：</label>
            <select id="questionType">
                <option value="voice">语音</option>
                <option value="text">文本</option>
            </select><br><br>
        </div>
        <button id="qaConfirm">确认</button>
    `;

    modal.style.display = 'block';

    document.getElementById('qaConfirm').addEventListener('click', sendPostRequest);
}

async function sendPostRequest() {
    const imageFile = document.getElementById('questionImage').files[0];
    const questionText = document.getElementById('questionText').value;
    const questionType = document.getElementById('questionType').value;

    // modal.style.display = 'none';

    modal.style.display = 'block';
    modalBody.innerText = '正在处理，请稍候...';

    const formData = new FormData();
    if (imageFile) {
        formData.append('image', imageFile);
    }
    formData.append('text', questionText);
    formData.append('type', questionType);

    try {
        const response = await fetch('/api/qa', {
            method: 'POST',
            headers: {
                
            },
            body: formData,
        });

        if (!response.ok) {
            modalBody.innerText = `错误: ${response.statusText}`;
            return;
        }

        const reader = response.body.getReader();
        const decoder = new TextDecoder('utf-8');
        modalBody.innerText = '';

        while (true) {
            const { value, done } = await reader.read();
            if (done) break;
            modalBody.innerText += decoder.decode(value, { stream: true });
        }
    } catch (error) {
        modalBody.innerText = `请求失败: ${error.message}`;
    }
}
